package com.bootdo.hjh.dao;

import com.bootdo.hjh.dao.provider.OrderProvider;
import com.bootdo.hjh.domain.OrderDO;
import com.bootdo.hjh.domain.OrderDOExample;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Mapper
public interface OrderDOMapper {
    int countByExample(OrderDOExample example);

    int deleteByExample(OrderDOExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(OrderDO record);

    int insertSelective(OrderDO record);

    List<OrderDO> selectByExampleWithRowbounds(OrderDOExample example, RowBounds rowBounds);

    List<OrderDO> selectByExample(OrderDOExample example);

    OrderDO selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") OrderDO record, @Param("example") OrderDOExample example);

    int updateByExample(@Param("record") OrderDO record, @Param("example") OrderDOExample example);

    int updateByPrimaryKeySelective(OrderDO record);

    int updateByPrimaryKey(OrderDO record);

    Long sumByExample(OrderDOExample example);

    void batchInsert(@Param("items") List<OrderDO> items);

    @Update("update t_order set order_status = 1 ,pay_channel = #{payChannel} where order_no = #{orderNo}")
    public Integer updateOrderPaid(@Param("orderNo") String orderNo,@Param("payChannel") String payChannel );


    @Select("select * from t_order where order_no = #{orderNo}")
    public OrderDO getOrderByOrderNo(String orderNo);

    @Update("update t_order set order_no  = concat(order_no,'0') where id = #{orderId}")
    public Integer updateOrderNo(Integer orderId);

    @Update("update t_order set is_fr = #{fr} where id = #{orderId}")
    int updateFrStatus(@Param("fr") Integer fr,@Param("orderId") Integer orderId);

    @Update("update t_order set fr_user_gain = #{frUserGain} where id = #{orderId}")
    int updateFrUserGain(@Param("frUserGain") Integer frUserGain,@Param("orderId") Integer orderId);

    @Update("update t_order set amount  = 1 where order_no = #{orderNo}")
    public Integer updateOrderAmount(String orderNo);

    @SelectProvider(type=OrderProvider.class,method ="queryOrder")
    List<OrderDO> queryOrder(@Param("offset") Integer offset, @Param("limit") Integer limit, @Param("orderNo") String
            orderNo,@Param("status") Integer status,@Param("mobile") String mobile,@Param("receiver") String receiver);

    @SelectProvider(type=OrderProvider.class,method ="countOrder")
    int countOrder(@Param("orderNo") String
            orderNo,@Param("status") Integer status,@Param("mobile") String mobile,@Param("receiver") String receiver);

    @Update("update t_order set order_status=#{status},last_modify_time=now() where id=#{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") Integer status);

    /**
     * 获取补货订单12小时为发货需要修改分润人的订单
     * @return
     */
    @Select("select * from t_order where order_no like 'B%' and order_status > 0 and fr_user_gain = 0 and is_fr = 1")
    List<OrderDO> getBuhuoOrder();


    @Update("update t_order set shipping_time=now() where id=#{orderId}")
    int updateBuhuoDeliverClearTime(@Param("orderId") Integer orderId);

    @Select("select shipping_time from t_order where id=#{orderId}")
    Date getBuhuoDeliverClearTime(@Param("orderId") Integer orderId);

    @Update("update t_order set shipping_time=now(), fr_user_id=#{frUserId} where id=#{orderId}")
    int updateBuhuoDeliverClearTimeAndFrUser(@Param("orderId") Integer orderId, @Param("frUserId") Long frUserId);

    /**
     * 获取补货订单12小时为发货需要修改分润人的订单
     * @return
     */
    @Select("select * from t_order where fr_user_id = #{userId} and order_status > 0 and fr_user_gain = 1 and is_fr ="
            + " 1 and product_id = #{productId}")
    List<OrderDO> getNonFrOrder(@Param("userId") Long userId, @Param("productId") Integer productId);

    /**
     * 查询我的待发货订单
     * @param userId
     * @return
     */
    @Select("select * from t_order where order_no like 'B%' and order_status > 0 and fr_user_gain = 0 and fr_user_id "
            + "= #{userId} and product_id in (5,6) order by id desc")
    List<OrderDO> getMyDeliverOrder(@Param("userId") Long userId);

}
